package com.cdd.user.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cdd.user.mapper.UserMapper;
import com.cdd.user.openFeign.RoleFeign;
import com.cdd.user.service.UserService;
import com.cdd.user.vo.UserVo;
import com.common.entity.Role;
import com.common.entity.User;
import com.common.untils.CommonResult;

import org.apache.shiro.crypto.hash.SimpleHash;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * TODO
 *
 * @author lenovo
 * @version 1.0
 * @since 2022-09-03  18:49:06
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RoleFeign roleFeign;

    //查询全部
    @Override
    public CommonResult userAll(Integer currentPage, Integer pageSize, UserVo userVo) {
        Page<User> page=new Page<>(currentPage,pageSize);
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(userVo.getUsername())){
            wrapper.like("username",userVo.getUsername());
        }
        if (StringUtils.hasText(userVo.getGmtCreate())){
            wrapper.ge("gmt_modified",userVo.getGmtCreate());
        }
        if (StringUtils.hasText(userVo.getGmtModified())){
            wrapper.le("gmt_modified",userVo.getGmtModified());
        }
        Page<User> page1 = userMapper.selectPage(page, wrapper);
        return new CommonResult(2000,"查询成功",page);
    }
    //删除员工
    @Override
    public CommonResult deleteUserById(String userId) {
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        System.out.println(userId+"====");
        wrapper.eq("user_id",userId);
        int delete = userMapper.delete(wrapper);
        if (delete==1){
            return new CommonResult(2000,"删除成功",null);
        }
        return new CommonResult(5000,"删除失败",null);
    }
    //修改密码
    @Override
    public CommonResult updateUserPassword(Map updatePasswordDate, String token) {
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String s = opsForValue.get(token);
        Map parseObject = JSONObject.parseObject(s, Map.class);
        JSONObject user = (JSONObject) parseObject.get("user");
        String password = user.getString("password");
        Object id = user.get("id");
        String salt = user.getString("salt");
        SimpleHash simpleHash = new SimpleHash("MD5",updatePasswordDate.get("oldPassword"),salt,1024);
        String s1 = simpleHash.toString();
        if (s1.equals(password)){
            SimpleHash simpleHash1 = new SimpleHash("MD5",updatePasswordDate.get("newPassword"),salt,1024);
            String s2 = simpleHash1.toString();
            User user1 = new User();
            user1.setPassword(s2);
            QueryWrapper<User> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",id);
            int update = userMapper.update(user1, queryWrapper);
            if (update!=0) {
                return new CommonResult(2000, "修改成功", null);
            }
        }
        return new CommonResult(5000,"原密码输入错误",null);
    }
    //添加员工
    @Override
    public CommonResult addUserByUser(User user) {
        //判断是否有username
        QueryWrapper<User> wrapper=new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        wrapper.eq("username",user.getUsername());
        User u = userMapper.selectOne(wrapper);
        if (u==null) {
            User user1=new User();
            user1.setUsername(user.getUsername());
            user1.setName(user.getName());
            user1.setIsDeleted(0);
            user1.setState(0);
            String salt= UUID.randomUUID().toString().replace("-","");
            user1.setSalt(salt);
            String password = user.getPassword();
            SimpleHash simpleHash = new SimpleHash("MD5",password,salt,1024);
            String s1 = simpleHash.toString();
            user1.setPassword(s1);
            int insert = userMapper.insert(user1);
            if (insert==1){
                return new CommonResult(2000,"添加成功",null);
            }else {
                return new CommonResult(5000,"添加失败",null);
            }
        }else {
            return new CommonResult(5001,"该用户已存在",null);
        }

    }
    //修改状态
    @Override
    public CommonResult updateUserState(String userId, Integer state) {
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        User user=new User();
        if (state == 0) {
            user.setState(state);
        }else if (state == 1){
            user.setState(state);
        }
        wrapper.eq("user_id",userId);
        int update = userMapper.update(user, wrapper);
        if (update ==1) {
            return new CommonResult(2000,"修改状态成功",null);
        }else {
            return new CommonResult(5000,"修改状态失败",null);
        }
    }
    //修改员工
    @Override
    public CommonResult updateUser(User user) {
        QueryWrapper<User> wrapper=new QueryWrapper();
        wrapper.eq("is_deleted",0);
        wrapper.eq("user_id",user.getUserId());
        User user1=new User();
        user1.setName(user.getName());
        int update = userMapper.update(user1, wrapper);
        if (update == 1) {
            return new CommonResult(2000,"修改成功",null);
        }else {
            return new CommonResult(5000,"修改失败",null);
        }
    }
    //设置角色
    @Override
    public CommonResult findRoleByUser(String userId) {
        //查询角色全部
        List<Role> listAll = roleFeign.ApiSelectRoleALl();
        //根据用户id查询角色
        List<Map> list = userMapper.selectListByUserId(userId);
        Map map=new HashMap();
        map.put("listAll",listAll);
        map.put("list",list);
        return new CommonResult(2000,"查询角色成功",map);
    }
    //分页查询
    @Override
    public CommonResult findRoleByRoleName(Integer roleCurrentPage, Integer rolePageSize, Role role) {
        CommonResult commonResult = roleFeign.selectAllRoleName(roleCurrentPage, rolePageSize, role);
        System.out.println(commonResult+"==========");
        return new CommonResult(2000,"查询成功",commonResult.getData());
    }
}
